<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取数据库实例</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.connecting.html">连接数据库</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.collection.html">获取集合实例</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.tutorial.selectdb" class="section">
   <h2 class="title">获取数据库实例</h2>
   <p class="para">
    要选择数据库，使用：
   </p>
   <div class="example" id="mongo.tutorial.selectdb-example">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbname</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <p class="para">
    这个数据库不需要提前建好，当你使用它的时候，就会自动建立。
   </p>
   <p class="para">
    要小心的是，你可能不小心建立了一个新的数据库，然后产生奇怪的错误。
    （在下面的例子中，第二次使用“同一个”数据库时 <em>name</em> 被错误的拼写成 <em>anme</em>）
    <div class="example" id="mongo.tutorial.selectdb.typo">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">mybiglongdbname</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;do&nbsp;some&nbsp;stuff<br /><br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">mybiglongdbanme</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;now&nbsp;connected&nbsp;to&nbsp;a&nbsp;different&nbsp;database!&nbsp;注意此时选择了另一个数据库！<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <div class="section" id="mongo.tutorial.selectdb.seealso">
    <h2 class="title">参见</h2>
    <p class="para">
     <a href="class.mongodb.html" class="classname">MongoDB</a>
     类的文档中有对数据库对象的详细说明。
    </p>
   </div>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.connecting.html">连接数据库</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.collection.html">获取集合实例</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
